using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using UnityEngine.UI;
using System.Drawing;



namespace YF.webGL.Renderer.Div
{

    // public class UnityIconsViewer : EditorWindow
    // {
    //     [MenuItem(("DT Tools/Unity内置图标"))]
    //     private static void ShowWindow()
    //     {
    //         var window = GetWindow<UnityIconsViewer>();
    //         window.minSize = window.maxSize = new Vector2(1280, 720);
    //         window.titleContent = new GUIContent("Unity内置图标", EditorGUIUtility.IconContent("d_UnityLogo").image);
    //     }

    //     public void CreateGUI()
    //     {
    //         //Assets/DTDiv/Shaders/Working/WebGL/Editor/UnityIconsViewer.cs
    //         var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/DTDiv/Shaders/Working/WebGL/Editor/UnityIconsWindow.uss");
    //         rootVisualElement.styleSheets.Add(styleSheet);

    //         var texture2Ds = Resources.FindObjectsOfTypeAll<Texture2D>();

    //         var container = new VisualElement();
    //         container.AddToClassList("container");

    //         for (int i = 0; i < texture2Ds.Length; i++)
    //         {
    //             var iconName = texture2Ds[i].name;
    //             var texture = EditorGUIUtility.IconContent(iconName).image;
    //             if (texture is Texture2D texture2D)
    //             {
    //                 var image = new VisualElement();
    //                 image.style.backgroundImage = new StyleBackground(texture2D);
    //                 image.AddToClassList("container-item");
    //                 container.Add(image);

    //                 image.RegisterCallback<ClickEvent>(evt =>
    //                 {
    //                     var copyStr = $"EditorGUIUtility.IconContent({iconName}).image";
    //                     GUIUtility.systemCopyBuffer = copyStr;
    //                     Debug.Log($"已复制到剪切板:{iconName}");
    //                 });
    //             }
    //         }

    //         var scrollView = new ScrollView();
    //         scrollView.Add(container);

    //         rootVisualElement.Add(scrollView);
    //     }
    // }

    class MyWindow : EditorWindow
    {
        [MenuItem("DT Tools/Unity内置图标 II")]
        public static void ShowWindow()
        {
            EditorWindow.GetWindow(typeof(MyWindow));
            // text = Resources.Load<TextAsset>("t").text.Split("\n"[0]);
        }

        public Vector2 scrollPosition;
        // private List<(string name, Texture texture)> allIcons;

        void OnEnable()
        {
            // allIcons = GetAllBuiltInIcons();
        }

        void OnGUI()
        {

            scrollPosition = GUILayout.BeginScrollView(scrollPosition);

            //鼠标放在按钮上的样式
            foreach (MouseCursor item in Enum.GetValues(typeof(MouseCursor)))
            {
                GUILayout.Button(Enum.GetName(typeof(MouseCursor), item));
                EditorGUIUtility.AddCursorRect(GUILayoutUtility.GetLastRect(), item);
                GUILayout.Space(10);
            }
            var texture2Ds = Resources.FindObjectsOfTypeAll<Texture2D>();
            //内置图标
            for (int i = 0; i < texture2Ds.Length; i += 8)
            {
                GUILayout.BeginHorizontal();
                for (int j = 0; j < 8; j++)
                {
                    int index = i + j;
                    if (index < texture2Ds.Length)
                    {
                        var iconName = texture2Ds[index].name;
                        var texture = EditorGUIUtility.IconContent(iconName).image;
                        GUIContent L = new GUIContent("", texture);
                        if (texture == null)
                        {
                            L.text = iconName;
                            continue;
                        }
                        if (GUILayout.Button(L, GUILayout.Width(100), GUILayout.Height(100)))
                        {
                            Debug.Log(iconName);
                        }
                    }

                }
                GUILayout.EndHorizontal();
            }




            GUILayout.EndScrollView();
        }

        private List<(string name, Texture texture)> GetAllBuiltInIcons()
        {
            List<(string name, Texture texture)> icons = new List<(string name, Texture texture)>();
            Type editorGUIType = typeof(EditorGUIUtility);
            MethodInfo iconContentMethod = editorGUIType.GetMethod("IconContent", new Type[] { typeof(string) });

            // 尝试 1000 个可能的图标名称
            for (int i = 0; i < 1000; i++)
            {
                string iconName = $"sv_icon_{i:D3}";
                object result = iconContentMethod.Invoke(null, new object[] { iconName });
                if (result != null)
                {
                    PropertyInfo imageProperty = result.GetType().GetProperty("image");
                    if (imageProperty != null)
                    {
                        Texture texture = imageProperty.GetValue(result) as Texture;
                        if (texture != null)
                        {
                            icons.Add((iconName, texture));
                        }
                    }
                }
            }

            return icons;
        }

        static string[] text = new string[]
        {
            "TreeEditor.AddLeaves",
            "TreeEditor.AddBranches",
            "TreeEditor.Trash",
            "TreeEditor.Duplicate",
            "TreeEditor.Refresh",
            "editicon.sml",
            "tree_icon_branch_frond",
            "tree_icon_branch",
            "tree_icon_frond",
            "tree_icon_leaf",
            "tree_icon",
            "animationvisibilitytoggleon",
            "animationvisibilitytoggleoff",
            "MonoLogo",
            "AgeiaLogo",
            "AboutWindow.MainHeader",
            "Animation.AddEvent",
            "lightMeter/greenLight",
            "lightMeter/lightRim",
            "lightMeter/orangeLight",
            "lightMeter/redLight",
            "Animation.PrevKey",
            "Animation.NextKey",
            "Animation.AddKeyframe",
            "Animation.EventMarker",
            "Animation.Play",
            "Animation.Record",
            "AS Badge Delete",
            "AS Badge Move",
            "AS Badge New",
            "WelcomeScreen.AssetStoreLogo",
            "preAudioAutoPlayOff",
            "preAudioAutoPlayOn",
            "preAudioPlayOff",
            "preAudioPlayOn",
            "preAudioLoopOff",
            "preAudioLoopOn",
            "AvatarInspector/BodySilhouette",
            "AvatarInspector/HeadZoomSilhouette",
            "AvatarInspector/LeftHandZoomSilhouette",
            "AvatarInspector/RightHandZoomSilhouette",
            "AvatarInspector/Torso",
            "AvatarInspector/Head",
            "AvatarInspector/LeftArm",
            "AvatarInspector/LeftFingers",
            "AvatarInspector/RightArm",
            "AvatarInspector/RightFingers",
            "AvatarInspector/LeftLeg",
            "AvatarInspector/RightLeg",
            "AvatarInspector/HeadZoom",
            "AvatarInspector/LeftHandZoom",
            "AvatarInspector/RightHandZoom",
            "AvatarInspector/DotFill",
            "AvatarInspector/DotFrame",
            "AvatarInspector/DotFrameDotted",
            "AvatarInspector/DotSelection",
            "SpeedScale",
            "AvatarPivot",
            "Avatar Icon",
            "Mirror",
            "AvatarInspector/BodySIlhouette",
            "AvatarInspector/BodyPartPicker",
            "AvatarInspector/MaskEditor_Root",
            "AvatarInspector/LeftFeetIk",
            "AvatarInspector/RightFeetIk",
            "AvatarInspector/LeftFingersIk",
            "AvatarInspector/RightFingersIk",
            "BuildSettings.SelectedIcon",
            "SocialNetworks.UDNLogo",
            "SocialNetworks.LinkedInShare",
            "SocialNetworks.FacebookShare",
            "SocialNetworks.Tweet",
            "SocialNetworks.UDNOpen",
            "Clipboard",
            "Toolbar Minus",
            "ClothInspector.PaintValue",
            "EditCollider",
            "EyeDropper.Large",
            "ColorPicker.CycleColor",
            "ColorPicker.CycleSlider",
            "PreTextureMipMapLow",
            "PreTextureMipMapHigh",
            "PreTextureAlpha",
            "PreTextureRGB",
            "Icon Dropdown",
            "UnityLogo",
            "Profiler.PrevFrame",
            "Profiler.NextFrame",
            "GameObject Icon",
            "Prefab Icon",
            "PrefabNormal Icon",
            "PrefabModel Icon",
            "ScriptableObject Icon",
            "sv_icon_none",
            "PreMatLight0",
            "PreMatLight1",
            "Toolbar Plus",
            "Camera Icon",
            "PreMatSphere",
            "PreMatCube",
            "PreMatCylinder",
            "PreMatTorus",
            "PlayButton",
            "PauseButton",
            "HorizontalSplit",
            "VerticalSplit",
            "BuildSettings.Web.Small",
            "js Script Icon",
            "cs Script Icon",
            "boo Script Icon",
            "Shader Icon",
            "TextAsset Icon",
            "AnimatorController Icon",
            "AudioMixerController Icon",
            "RectTransformRaw",
            "RectTransformBlueprint",
            "MoveTool",
            "MeshRenderer Icon",
            "Terrain Icon",
            "SceneviewLighting",
            "SceneviewFx",
            "SceneviewAudio",
            "SettingsIcon",
            "TerrainInspector.TerrainToolRaise",
            "TerrainInspector.TerrainToolSetHeight",
            "TerrainInspector.TerrainToolSmoothHeight",
            "TerrainInspector.TerrainToolSplat",
            "TerrainInspector.TerrainToolTrees",
            "TerrainInspector.TerrainToolPlants",
            "TerrainInspector.TerrainToolSettings",
            "RotateTool",
            "ScaleTool",
            "RectTool",
            "MoveTool On",
            "RotateTool On",
            "ScaleTool On",
            "RectTool On",
            "ViewToolOrbit",
            "ViewToolMove",
            "ViewToolZoom",
            "ViewToolOrbit On",
            "ViewToolMove On",
            "ViewToolZoom On",
            "StepButton",
            "PlayButtonProfile",
            "PlayButton On",
            "PauseButton On",
            "StepButton On",
            "PlayButtonProfile On",
            "PlayButton Anim",
            "PauseButton Anim",
            "StepButton Anim",
            "PlayButtonProfile Anim",
            "WelcomeScreen.MainHeader",
            "WelcomeScreen.VideoTutLogo",
            "WelcomeScreen.UnityBasicsLogo",
            "WelcomeScreen.UnityForumLogo",
            "WelcomeScreen.UnityAnswersLogo",
            "Toolbar Plus More"
        };
    }


}
